package com.menu.Service.Impl;

import com.menu.Mapper.GoodsMapper;
import com.menu.Mapper.OrderMapper;
import com.menu.Pojo.GoodsVo;
import com.menu.Service.TimetaskService;
import com.menu.Utils.RedisKeyUtils;
import com.menu.Utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Component
public class TimetaskServiceImpl implements TimetaskService {

    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private OrderMapper orderMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    @Scheduled(cron = "0 0 1 * * ?")
    public void pageView() throws InterruptedException {
        log.info("开始同步点击量：{}", LocalDate.now());
        Map<Object, Object> iptotal = redisUtil.hmget(RedisKeyUtils.MAP_KEY_GOODS);
        redisUtil.del(RedisKeyUtils.MAP_KEY_GOODS);
        Set<Object> keySet = iptotal.keySet();
        Iterator<Object> iterator = keySet.iterator();
        Integer countTotal = 0;
        while(iterator.hasNext()){
            Object object = iterator.next();
            Integer temp = (Integer) iptotal.get(object);
            countTotal += temp;
        }
        goodsMapper.insertVisit(countTotal,0);
        redisUtil.del(RedisKeyUtils.MAP_KEY_GOODS);
        log.info("点击量同步完毕：{}", LocalDate.now());
    }

    @Override
    @Scheduled(cron = "0 10 1 * * ?")
    public void pageSales() {
        log.info("开始同步销售量：{}", LocalDate.now());
        Object sum = redisUtil.get(RedisKeyUtils.DAY_OF_SALES);
        redisUtil.del(RedisKeyUtils.GOODS_SALES);
        Integer temp = null;
        if (sum == null){
            temp = 0;
        }else{
            temp = (Integer) sum;
        }
        goodsMapper.insertVisit(temp,1);
        redisUtil.del(RedisKeyUtils.DAY_OF_SALES);
        log.info("销售量同步完毕：{}", LocalDate.now());
    }

    @Override
    @Scheduled(cron = "0 0/30 * * * ?")
    public void goodsSaleRate() {
        log.info("开始更新最新排行");
        redisUtil.del(RedisKeyUtils.GOODS_SALES_RATE);
        List<GoodsVo> list = orderMapper.goodsSaleRate();
        redisUtil.set(RedisKeyUtils.GOODS_SALES_RATE,list);
        log.info("最新排行更新完毕");
    }

    @Override
    @Scheduled(cron = "0 0/15 * * * ?")
    public void realData() {
        redisUtil.del(RedisKeyUtils.REAL_DATE);
    }

    @Override
    @Scheduled(cron = "0 0/3 * * * ?")
    public void flushVisit() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        Integer i = calendar.get(Calendar.HOUR_OF_DAY);
        Long l = redisUtil.sGetSetSize(RedisKeyUtils.VISIT_NUMBER_IP);
        if (l == null){
            l = 0L;
        }
        redisUtil.hset(RedisKeyUtils.VISIT_COUNT_HOUR,i.toString(),l);
    }

    @Scheduled(cron = "0 1 0 * * ?")
    @Override
    public void syncToDabase() throws InterruptedException {
        log.info("开始访问量同步至数据库{}",LocalDateTime.now());
        Map<Object, Object> hmget = redisUtil.hmget(RedisKeyUtils.VISIT_COUNT_HOUR);
        redisUtil.del(RedisKeyUtils.VISIT_COUNT_HOUR);
        Thread.sleep(1000);
        redisUtil.del(RedisKeyUtils.VISIT_NUMBER_IP);
        Set<Object> keySet = hmget.keySet();
        Integer temp = 0;
        for (Object object:keySet){
            temp += (Integer) hmget.get(object);
        }
        Integer integer = goodsMapper.insertIpVisit(temp);
        if (integer == null){
            log.info("同步失败！");
        }
        redisUtil.del(RedisKeyUtils.VISIT_NUMBER_IP);
        log.info("访问量同步完成{}",LocalDateTime.now());
    }

    @Override
    @Scheduled(cron = "0 0 23 * * SUN")
    public void turnoverWeekRatio() {
        log.info("开始更新周销售率{}",LocalDateTime.now());
        Long total = orderMapper.getLimitData(null,7);
        Long finish = orderMapper.getLimitData(1,7);
        total=(total == null?0L:total);
        double result = 0.0;
        if (finish != null){
            result = finish * 1.0 /total;
        }
        Integer integer = orderMapper.turnoverRatio(0, result);
        if (integer == 1){
            log.info("同步成功！");
        }
        log.info("更新周销售率完成{}",LocalDateTime.now());
    }

    @Override
    @Scheduled(cron = "0 0 23 28-31 * ?")
    public void turnoverMonthRatio() {
        log.info("开始更新月销售率{}",LocalDateTime.now());
        final Calendar c = Calendar.getInstance();
        if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) {
            Long total = orderMapper.getLimitData(null,c.get(Calendar.DATE));
            Long finish = orderMapper.getLimitData(1,c.get(Calendar.DATE));
            total=(total == null?0L:total);
            double result = 0.0;
            if (finish != null){
                result = finish * 1.0 /total;
            }
            Integer integer = orderMapper.turnoverRatio(1, result);
            if (integer == 1){
                log.info("同步成功！");
            }
        }
        log.info("更新月销售率完成{}",LocalDateTime.now());
    }

    @Override
    @Scheduled(cron = "0 0 0 31 12 ?")
    public void turnoverYearRatio() {
        log.info("开始更新年销售率{}",LocalDateTime.now());
        Integer day = LocalDate.now().lengthOfYear();
        Long total = orderMapper.getLimitData(null,day);
        Long finish = orderMapper.getLimitData(1,day);
        total=(total == null?0L:total);
        double result = 0.0;
        if (finish != null){
            result = finish * 1.0 /total;
        }
        Integer integer = orderMapper.turnoverRatio(2, result);
        if (integer == 1){
            log.info("同步成功！");
        }
        log.info("更新年销售率完成{}",LocalDateTime.now());
    }

    @Override
    @Scheduled(cron = "0 0/10 * * * ?")
    public void deleteHotWord() {
        Set<Object> result = redisUtil.rangeZset(RedisKeyUtils.ALL_IN_SEARCH,0L,12L);
        if (result.size() <= 12){
            return;
        }else{
            redisUtil.removeZset(RedisKeyUtils.ALL_IN_SEARCH,12,-1);
        }
    }
}
